package com.javakc.mes.monthly.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javakc.commonutils.api.R;
import com.javakc.mes.monthly.entity.Monthly;
import com.javakc.mes.monthly.service.MonthlyService;
import com.javakc.servicebase.config.InstructionQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "月份计划")
@RestController
@RequestMapping("mes/monthly")
public class MonthlyController {

    @Autowired
    private MonthlyService monthlyService;

    @GetMapping
    @ApiOperation(value = "查询所有记录")
    public R findAll(){
        List<Monthly> list = monthlyService.list();
        return R.OK().data("items",list);
    }

    @ApiOperation(value = "根据条件进行分页查询")
    @GetMapping("{pageNo}/{pageSize}")
    public R pageMonthly(
            @ApiParam(name = "pageNo",value = "页码",required = true)
            @PathVariable("pageNo")Long pageNo,
            @ApiParam(name = "pageSize",value = "每页最大数",required = true)
            @PathVariable("pageSize")Long pageSize,
            @ApiParam(name = "instructionQuery",value = "查询条件封装对象",required = false)
            @RequestBody(required = false)InstructionQuery instructionQuery){
        Page<Monthly> monthlyPage = new Page<>(pageNo,pageSize);
        monthlyService.findPageMonthlyPage(monthlyPage,instructionQuery);
        long total = monthlyPage.getTotal();
        List<Monthly> monthlyList = monthlyPage.getRecords();
        return  R.OK().data("total",total).data("items",monthlyList);
    }

    @ApiOperation(value = "根据条件进行单条数据查询")
    @GetMapping("getMonthlyById/{monthlyId}")
    public R getMonthlyById(@PathVariable("monthlyId") int monthlyId){
        Monthly monthly = monthlyService.getById(monthlyId);
        return R.OK().data("items",monthly);
    }

    @ApiOperation(value = "添加数据")
    @PostMapping("saveMonthly")
    public R saveMonthly(@RequestBody Monthly monthly){
        boolean flag = monthlyService.save(monthly);
        if (flag)
            return R.OK();
        else
            return  R.ERROR();
    }

    @ApiOperation(value = "修改月度计划")
    @PutMapping("updateMonthly")
    public R updateMonthly(@RequestBody Monthly monthly){
        boolean flag = monthlyService.updateById(monthly);
        if(flag)
            return  R.OK();
        else
            return R.ERROR();
    }

    @ApiOperation(value = "根据ID删除月度计划数据")
    @DeleteMapping("deleteMonthly/{monthlyId}")
    public R deleteMonthly(@PathVariable("monthlyId") String monthlyId){
        boolean flag = monthlyService.removeById(monthlyId);
        if (flag)
            return R.OK();
        else
            return R.ERROR();
    }
}
